
package a4;

import data.Offer;
import data.Order;
import data.Product;
import databases.UserList;
import java.io.File;
import java.util.List;
import user.Administrator;
import user.Customer;
import user.Employee;
import user.GeneralUser;

/*
 * @author Γκαμπέρλο Ναϊμ
 * @author Μώτος Διονύσης
 * @author Παπαδημητρίου Δημήτρης
 * @author Παπαδόπουλος Αλέξανδρος p3080138
 * @author Παπαδόπουλος Λεωνίδας p3080139
 */

/**
 * 
 * @author dionysis2
 */
public class Controller {

    
    
    /**
     * GeneralUser checkLogin(String name, String pass)
     * 
     * "Elegxos twn stoixeiwn tou xrhsth.Xrhsimopoihtai apo to LoginUI gia to authentication.
     * Epistrefei ena antikeimeno Administrator,Employee h Customer,analoga thn peristash
     * gia epityxes login,h null gia anepityxes."
     * 
     * @param name
     * @param pass
     * @return null An ta stoixeia dn einai swsta alliws GeneralUser
     */
    public static GeneralUser checkLogin(String name, String pass) {

        for (int i = 0; i < Main.userList.size(); i++) {
            if (((Main.userList.get(i).getEmail())).equals(name)) {
                if (((Main.userList.get(i).getPassword())).equals(pass)) {
                    return Main.userList.get(i);
                }
            }
        }
        return null;
    }

    

    /**
     * boolean emailExists(String name)
     * 
     * Elegxos tou UserName pou 8a dwsei o Xrhsths,oti den einai hdh se xrhsh.
     * 
     * emailExists(String name)
     * @param name
     * @return
     */
    public static boolean emailExists(String name) {
         UserList  users=Main.userList;
        for (int i = 0; i < users.size(); i++) {
            if (((users.get(i).getEmail())).equals(name)) {
                return true;
            }
        }
        if (name.equals("admin")) {
            return true;
        }
        return false;
    }

    

    
    /**
     * void addOffer(float[] prices, float price, String[] arrayOfOffers, String prosfora) {
     * 
     * H addOffer einai mia me8odos pou kaleitai apo to UI tou Employee gia th
     * dhmiourgia neas prosforas.Apla pros8etei ston pinaka twn prosforwn thn nea
     * prosfora
     * 
     * @param prices O pinakas twn timwn twn prosforwn
     * @param price H timh panw apo thn opoia 8a dinetai h nea prosfora
     * @param arrayOfOffers O pinakas twn proiontwn twn prosforwn
     * @param prosfora  To proion pou 8a dinetai me thn nea prosfora
     */
    public static void addOffer(float[] prices, float price, String[] arrayOfOffers, String prosfora) {

        for (int i = 1; i < 4; i++) {
            if ((prices[i] != (float) -1.0) && (prices[i] < price)) {
                prices[i - 1] = prices[i];
                arrayOfOffers[i - 1] = arrayOfOffers[i];
                prices[i] = -1;
                arrayOfOffers[i] = null;
                break;
            }
            else if ((prices[i] != (float) -1.0) && (prices[i] >= price)) {
                prices[i - 1] = price;
                arrayOfOffers[i - 1] = prosfora;
                break;
            }
            else if ((prices[i] == (float) -1.0) && (i == 3)) {
                prices[i] = price;
                arrayOfOffers[i] = prosfora;
                break;
            }
        }
    }

 
    
    /**
     * void removeOffer(int i, float[] prices, String[] arrayOfOffers, int diagrafh)
     * 
     * H removeOffer einai mia me8odos pou kaleitai apo to UI tou Employee gia th
     * diagrafh mias prosforas.Apla afairei apo ton pinaka twn prosforwn thn epilegmanh
     * prosfora
     * 
     * @param i H 8esh ths prosforas pros diagrafh
     * @param prices    O pinakas twn timwn twn prosforwn
     * @param arrayOfOffers O pinakas twn proiontwn twn prosforwn
     */
    public static void removeOffer(int i, float[] prices, String[] arrayOfOffers) {
        prices[i] = (float) -1.0;
        arrayOfOffers[i] = null;
        for (int j = i; j > -1; j--) {
            if (prices[j] != (float) -1.0) {
                prices[j + 1] = prices[j];
                arrayOfOffers[j + 1] = arrayOfOffers[j];
                prices[j] = (float) -1.0;
                arrayOfOffers[j] = null;
            }
        }

    }

    
    /**
     * void exit()
     * 
     * H Synartish pou kaleitai kata thn eksodo tou programmatos gia thn metafora 
     * twn dedomenwn,apo thn ram ston disko.
     */     
    public static void exit() {   
        Main.productList.exportToFile(new File("Products.ser"));
        Main.userList.exportToFile(new File("Users.ser"));
        Main.orderList.exportToFile(new File("Orders.ser"));
        Main.offerList.exportToFile(new File("Offers.ser"));
        System.exit(0);
    }
    

    /**
     * void load()
     * 
     * H Synartish pou kaleitai kata thn enarksh tou programmatos gia thn metafora 
     * twn dedomenwn,apo ton disko sthn ram.
     */  
    public static void load() {     
        Main.productList.importFromFile(new File("Products.ser"));
        Main.userList.importFromFile(new File("Users.ser"));
        Main.orderList.importFromFile(new File("Orders.ser"));
        Main.offerList.importFromFile(new File("Offers.ser"));
    }
    
    

    /**
     *  void removeOrder(Customer cust)
     * 
     * Diagrafh mias paragelias apo th lista.kaleitai toso apo to UI tou Employee
     * oso kai apo tou Customer
     * @param cust
     */
    public static void removeOrder(Customer cust) {
       
        for (int i = 0; i < Main.orderList.size(); i++) {
            if (Main.orderList.get(i).getCustomer().equals(cust.getEmail())) {
                Main.orderList.remove(i);
            }
        }
    }


    /**
     * boolean hasAnOrder(Customer cust)
     * 
     * Elegxos ean to sygkekrimenos pelaths exei hdh kanei paragelia
     * 
     * @param cust Customer
     * @return true an exei kanei paragelia
     */
    public static boolean hasAnOrder(Customer cust) {     
        for (int i = 0; i < Main.orderList.size(); i++) {
            if (Main.orderList.get(i).getCustomer().equals(cust.getEmail())) {
                return true;
            }
        }
        return true;
    }

    public static void deleteOffer(int diagrafh) {
        Main.offerList.remove(diagrafh);
    }
    
    
    /**
     * Offer getOffer(float price)
     * 
     * Epistrefei thn prosfora pou antistoixei sthn do8eisa timh 
     * 
     * @param price
     * @return null an den antistoixei kapoia prosfora sthn timh
     * 
     */
    public static Offer getOffer(List<Integer> selectedProducts) {
        float totalPrice = 0;
        for (int i=0; i < selectedProducts.size(); ++i)
            totalPrice += Main.productList.get(i).getCost();
        for (int i = Main.offerList.size()-1; i<=0; --i){
            Offer offer = Main.offerList.get(i);
            if (offer.getPrice()<= totalPrice)
                return offer;
        }
        return null;
    }
    

    public static void addEmployee(String name, String surname, String username,String pass,String tel,String address){
        Main.userList.add( new Employee(name, surname, tel, pass, tel, address) );
    }
    
    public static void addCustomer(String name,String surname, String email,String password,String tel,String address){
        Main.userList.add( new Customer(name, surname, email, password, tel, address));
        }
    
    public static void addAdministrator(String name,String surname, String email,String password,String tel,String address){
        Main.userList.add( new Administrator(name, surname, email, password, tel, address));
        }
    
    public static void deleteUser(int user){
        Main.userList.remove(user);
    }
    public static void removeProduct(int num){
        Main.productList.remove(num);
    }

    public static void addProduct(String name,String perigrafh,String cat,float cost){
        Product pro=new Product(name,perigrafh,cat,cost);
    }
    
    public static void addOffer(String item, float price){
        Main.offerList.add( new Offer(item, price ) );
    }
    public static void addOrder(Customer customer, List<Integer> selectedProducts, Offer offer){
        Order order = new Order(customer, offer);
        for (Integer i : selectedProducts)
            order.addProduct( Main.productList.get(i) );
    }
}
